package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import com.slipper.util.Tool;
import com.slipper.system.codeless.bo.DataModelBO;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFormFieldDao;
import com.slipper.system.codeless.bo.DataFormFieldBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataFormFieldService {
    @Resource
    private DataFormFieldDao dataFormFieldDao;

    public DataFormFieldBO queryBy(String id) {
        return dataFormFieldDao.queryBy(id);
    }
    
    public List<DataFormFieldBO> queryList(DataFormFieldBO bo) {
        return dataFormFieldDao.queryList(bo);
    }
    
    public PageInfo<DataFormFieldBO> queryPage(DataFormFieldBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFormFieldDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String modelId, List<DataFormFieldBO> list, String employeeId) {
        if (Tool.isBlank(modelId)) {
            return 0;
        }

        int i = 0;
        for (DataFormFieldBO field : list) {
            field.setModelId(modelId);
            field.setOrdinal(++i);
            if (Tool.isBlank(field.getId())) {
                field.setId(UUID.getUUID());
                field.setUpdateId(employeeId);
                field.setCreateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                field.setCreateTime(System.currentTimeMillis());
                dataFormFieldDao.insert(field);
            } else {
                field.setUpdateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                dataFormFieldDao.update(field);
            }
        }

        // 删除被删除的数据
        DataFormFieldBO qo = new DataFormFieldBO();
        qo.setModelId(modelId);
        dataFormFieldDao.queryList(qo).stream()
                .filter(field -> list.stream().noneMatch(temp -> field.getId().equals(temp.getId())))
                .forEach(field -> dataFormFieldDao.delete(field.getId()));

        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFormFieldBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFormFieldDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFormFieldDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFormFieldDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void createDefault(String modelId, DataModelBO bo) {
        long fieldSize = bo.getFieldList().stream()
                .filter(field -> !Constants.YES.equals(field.getBuiltin()))
                .count();
        bo.getFieldList().forEach(field -> {
            if (Constants.YES.equals(field.getBuiltin())) {
                return;
            }

            DataFormFieldBO formField = new DataFormFieldBO();
            formField.setId(UUID.getUUID());
            formField.setModelId(modelId);
            formField.setFieldId(field.getId());
            formField.setFieldName(field.getFieldName());
            formField.setAliasName(field.getShowName());
            formField.setLabelColor(null);
            formField.setDefaultValue(field.getDefaultValue());
            formField.setColCnt(fieldSize > 8 ? 11 : 22);
            formField.setHide(Constants.NO);
            formField.setOrdinal(field.getOrdinal());
            formField.setRequired(field.getRequired());
            formField.setUpdateId(bo.getUpdateId());
            formField.setCreateId(bo.getCreateId());
            formField.setUpdateTime(System.currentTimeMillis());
            formField.setCreateTime(System.currentTimeMillis());
            dataFormFieldDao.insert(formField);
        });
    }

    @WriteTransactional
    public void deleteByModelId(String modelId) {
        if (Tool.isBlank(modelId)) {
            return;
        }

        dataFormFieldDao.deleteByModelId(modelId);
    }

    @WriteTransactional
    public void copyByModelId(String oldModelId, String newModelId, String employeeId) {
        if (Tool.isBlank(oldModelId) || Tool.isBlank(newModelId)) {
            return;
        }

        DataFormFieldBO qo = new DataFormFieldBO();
        qo.setModelId(oldModelId);
        dataFormFieldDao.queryList(qo).forEach(field -> {
            field.setId(UUID.getUUID());
            field.setModelId(newModelId);
            field.setUpdateId(employeeId);
            field.setCreateId(employeeId);
            field.setUpdateTime(System.currentTimeMillis());
            field.setCreateTime(System.currentTimeMillis());
            dataFormFieldDao.insert(field);
        });
    }

    public List<DataFormFieldBO> queryByModelId(String modelId) {
        if (Tool.isBlank(modelId)) {
            return Collections.emptyList();
        }

        DataFormFieldBO qo = new DataFormFieldBO();
        qo.setModelId(modelId);
        return dataFormFieldDao.queryList(qo);
    }
}
